package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.TxnLimitStatisMapper;
import cc.rengu.oltp.service.common.entity.TxnLimitStatis;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 交易限额统计表操作接口实现类
 * Created by 王成 on 2020/2/6
 */
public class TxnLimitStatisMapperImpl implements TxnLimitStatisMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public TxnLimitStatisMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertTxnLimitStatis(TxnLimitStatis txnLimitStatis) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertTxnLimitStatis");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, txnLimitStatis, null);
        if (0 != iReturnCode) {
            rglog.error("登记交易限额统计信息失败,instId:<{}>,statisDate:<{}>,statisType:<{}>,statisPrimaryKey:<{}>,iReturnCode:<{}>",
                    txnLimitStatis.getInstId(), txnLimitStatis.getStatisDate(), txnLimitStatis.getStatisType(), txnLimitStatis.getStaticPrimaryKey(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateTxnLimitStatisByPrimaryKey(TxnLimitStatis txnLimitStatis) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateTxnLimitStatisByPrimaryKey");
        txnLimitStatis.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, txnLimitStatis, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新交易限额统计信息失败,instId:<{}>,statisDate:<{}>,statisType:<{}>,statisPrimaryKey:<{}>,iReturnCode:<{}>",
                    txnLimitStatis.getInstId(), txnLimitStatis.getStatisDate(), txnLimitStatis.getStatisType(), txnLimitStatis.getStaticPrimaryKey(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public TxnLimitStatis selectTxnLimitStatisByPrimaryKey(String instId, String statisDate, String statisType, String statisPrimaryKey) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTxnLimitStatisByPrimaryKey");
        TxnLimitStatis txnLimitStatis = new TxnLimitStatis();
        txnLimitStatis.setInstId(instId);
        txnLimitStatis.setStatisDate(statisDate);
        txnLimitStatis.setStatisType(statisType);
        txnLimitStatis.setStaticPrimaryKey(statisPrimaryKey);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, txnLimitStatis, txnLimitStatis);
        if (0 != iReturnCode) {
            rglog.warn("根据主键查询交易限额统计信息失败,instId:<{}>,statisDate:<{}>,statisType:<{}>,statisPrimaryKey:<{}>,iReturnCode:<{}>", instId, statisDate, statisType, statisPrimaryKey, iReturnCode);
            return null;
        }
        return txnLimitStatis;
    }

    @Override
    public List<TxnLimitStatis> selectTxnLimitStatisByLimitInst(String instId, String statisDate, String statisType, String limitInst) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTxnLimitStatisByLimitInst");
        TxnLimitStatis txnLimitStatis = new TxnLimitStatis();
        txnLimitStatis.setInstId(instId);
        txnLimitStatis.setStatisDate(statisDate);
        txnLimitStatis.setStatisType(statisType);
        txnLimitStatis.setLimitInst(limitInst);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, txnLimitStatis, TxnLimitStatis.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.warn("根据限额主体查询交易限额统计信息失败,instId:<{}>,statisDate:<{}>,statisType:<{}>,limitInst:<{}>", instId, statisDate, statisType, limitInst);
            return null;
        }
        return objectList.stream().map(item -> (TxnLimitStatis) item).collect(Collectors.toList());
    }

    @Override
    public TxnLimitStatis selectTxnLimitStatisByLimitInstAndAcctType(String instId, String statisDate, String statisType, String limitType, String limitInst, String limitAcctType) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTxnLimitStatisByLimitInstAndAcctType");
        TxnLimitStatis txnLimitStatis = new TxnLimitStatis();
        txnLimitStatis.setInstId(instId);
        txnLimitStatis.setStatisDate(statisDate);
        txnLimitStatis.setStatisType(statisType);
        txnLimitStatis.setLimitType(limitType);
        txnLimitStatis.setLimitInst(limitInst);
        txnLimitStatis.setLimitAcctType(limitAcctType);
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, txnLimitStatis, txnLimitStatis);
        if (0 != iReturnCode) {
            rglog.warn("根据限额类型、限额主体及限额账户类型查询交易限额统计信息失败,instId:<{}>,statisDate:<{}>,statisType:<{}>,limitType:<{}>,limitInst:<{}>,limitAcctType:<{}>,iReturnCode:<{}>", instId, statisDate, statisType, limitType, limitInst, limitAcctType, iReturnCode);
            return null;
        }
        return txnLimitStatis;
    }
}
